package com.lancoo.rxretrofitlibrary.http;

import android.widget.Toast;


import com.lancoo.cpbase.authentication.base.CurrentUser;
import com.lancoo.cpbase.authentication.base.ExitBack;
import com.lancoo.cpbase.authentication.base.LoginBack;
import com.lancoo.cpbase.authentication.base.LoginOperate;
import com.lancoo.rxretrofitlibrary.Api.BaseApi;
import com.lancoo.rxretrofitlibrary.Api.BaseResultEntity;
import com.lancoo.rxretrofitlibrary.Api.UserInfo;
import com.lancoo.rxretrofitlibrary.exception.HttpTimeException;

import retrofit2.Retrofit;
import rx.Observable;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * toekan失效统一处理
 * Created by WZG on 2017/3/21.
 */

public class TokeanFunc<T extends BaseResultEntity> implements Func1<T, Observable<T>> {
    private BaseApi basePar;
    private Retrofit retrofit;
    private boolean isReLogin;

    public TokeanFunc(BaseApi basePar, Retrofit retrofit) {
        this.basePar = basePar;
        this.retrofit = retrofit;
    }

    @Override
    public Observable call(T o) {
             /*这里是判断错误tokean之类过期的错误，假设所有接口全部失败*/
            /*这里简单的以tokean为空来判断，实际运用中需要通过服务器错误标示来判断启用tokean机制*/


            if (!o.getIsSuccess() && o.getMessage().equals("token安全码验证失败！")){

                LoginOperate mOperate = new LoginOperate(basePar.getRxAppCompatActivity());
                int loginFlag = mOperate.reLogin();
                if (loginFlag == 1) {
                    UserInfo.Token = CurrentUser.Token;
                    return basePar.getObservable(retrofit);
                    //return Observable.error(new TimeoutException("token安全码验证失败！"));
                } else {
                    return Observable.error(new Exception("Token失效"));

                }
            }

            return Observable.just(o);

      /* isReLogin = false;

            if (!o.getIsSuccess() && o.getMessage().equals("token安全码验证失败！")){


                final MaterialDialog dialog = new MaterialDialog.Builder(basePar.getRxAppCompatActivity())
                        .customView(R.layout.dialog_token,false)
                        .cancelable(false)
                        .show();
                View view = dialog.getCustomView();
                view.findViewById(R.id.tv_returnTree).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });

                view.findViewById(R.id.tv_reviewNext).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        dialog.dismiss();
                        LoginOperate mOperate = new LoginOperate(getContext());
                        int loginFlag = mOperate.reLogin();
                        if (loginFlag == 1) {
                            basePar.setTokean(CurrentUser.Token);
                            isReLogin = true;
                        } else {
                            mOperate.openLoginDialog(new LoginBack() {
                                @Override
                                public void loginSuccess() {
                                    basePar.setTokean(CurrentUser.Token);
                                    isReLogin = true;
                                }
                            }, false, false, new ExitBack() {
                                @Override
                                public void exit() {

                                }
                            });
                        }
                    }
                });



            }
            if (isReLogin){
                Toast.makeText(basePar.getRxAppCompatActivity(), "重新登录成功！",Toast.LENGTH_LONG);
                return basePar.getObservable(retrofit);
            }else {
                return Observable.just(o);
            }*/
    }
}
